/*
 * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
 */
package org.emergent.antbite.savant.log;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * <p>
 * This class is the manager for all logging activities
 * inside the Savant build system. It maintains a list of
 * {@link LogListener}s that are called for log events. This
 * is also used be the Log class.
 * </p>
 *
 * @author Brian Pontarelli
 */
public class LogManager {

    private static final LogManager instance = new LogManager();
    private List listeners = new ArrayList();


    /**
     * Returns the singleton instance of the LogManager.
     *
     * @return  The LogManager and never null.
     */
    public static LogManager getInstance() {
        return instance;
    }


    /**
     * Adds the given listener to the list.
     */
    public void addListener(LogListener listener) {
        listeners.add(listener);
    }

    /**
     * Removes the given listener to the list.
     */
    public void removeListener(LogListener listener) {
        listeners.remove(listener);
    }

    /**
     * Returns the list of listeners as an unmodifiable list.
     */
    public List getListener() {
        return Collections.unmodifiableList(listeners);
    }

    /**
     * Sends the message to all the registered listeners.
     *
     * @param   msg The message to send.
     * @param   level The level of the log.
     */
    protected void sendMessage(String msg, String level) {
        for (int i = 0; i < listeners.size(); i++) {
            LogListener listener = (LogListener) listeners.get(i);
            listener.logged(msg, level);
        }
    }
}